package xpath;

import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.jaxen.JaxenException;
import org.jaxen.SimpleNamespaceContext;
import org.jaxen.XPath;
import org.jaxen.dom4j.Dom4jXPath;

public class XPathTest {

	public static void main(String[] args) throws DocumentException, JaxenException, Exception {
		File xmlFile = new File("src/main/java/xpath/appContext-jms.xml");

		SAXReader reader = new SAXReader();
		Document doc = reader.read(xmlFile);

		Map map = new HashMap();
		map.put("amq", "http://activemq.apache.org/schema/core");

		XPath xpath = new Dom4jXPath("//amq:transportConnector/@uri");
		xpath.setNamespaceContext(new SimpleNamespaceContext(map));
		
		System.out.println(xpath.selectSingleNode(doc));
		
		Attribute attr = (Attribute) xpath.selectSingleNode(doc);
		attr.setValue("123213123213213");
		
		XMLWriter output = new XMLWriter(new FileWriter("src/main/java/xpath/appContext-jms_output.xml"));
		output.write(doc);
		output.close();
	}

}
